<?php

/**
 * Loader plug-in to load and manage kit-style folders.
 */
class LdKitLoader implements ILdLoader {
	private $kits = array();
	
	function includeClassWithName($name) {
		foreach ($this->kits as $kitName => $dir) {
			if (is_file($f = ($dir . DIRECTORY_SEPARATOR . $name . '.php'))) {
				require $f;
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Register a just-loaded kit with the loading system.
	 * 
	 * @param name The name of the kit, which may be different from
	 * the name of the directory in the filesystem.
	 * @param dir The kit directory or one of the files directly found
	 * inside the kit directory. Usually just __FILE__.
	 */
	static function register($name, $dir) {
		if (is_file($dir))
			$dir = realpath(dirname($dir));
		
		LdKitLoader::instance()
			->kits[$name] = $dir;
	}
	
	static function pathForKit($kit) {
		return @(LdKitLoader::instance()
			->kits[$kit]);
	}
	
	/**
	 * Returns the instance of the global kit loader, creating it
	 * if it doesn't exist. This instance is already registered
	 * with the loading system.
	 * 
	 * There should not be a reason to call this method directly.
	 */
	static function instance() {
		static $self = null;
		if (!$self)
			$self = new LdKitLoader();
		return $self;
	}
}

LdAddLoader(LdKitLoader::instance());

?>